package com.yunhe.job.controller;

import com.yunhe.job.domain.ElasticJobConfigBean;
import com.yunhe.common.model.ResultObject;
import com.yunhe.job.runner.ElasticJobRunner;
import com.yunhe.job.service.ElasticJobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

/**
 * 任务配置Controller
 * @author liuronglei
 */
@Api(value = "elasticJob",description = "任务维护", tags = {"Job_ElasticJob"})
@RestController
@RequestMapping("/job")
public class ElasticJobController {

	@Autowired
	private ElasticJobService elasticJobService;

	@RequestMapping(method = RequestMethod.GET)
	public ResponseEntity<ResultObject> getElasticJobConfigList(@RequestParam("jobName") Optional<String> jobName) {
		Iterable<ElasticJobConfigBean> results = elasticJobService.getElasticJobConfigList(jobName.orElse(null));
		return new ResponseEntity<>(new ResultObject<>(results), HttpStatus.OK);
	}

	@RequestMapping(method = RequestMethod.POST)
	public ResponseEntity<ResultObject> addElasticJobConfig(@RequestBody ElasticJobConfigBean elasticJobConfigBean) {
		ElasticJobConfigBean result = elasticJobService.addElasticJobConfig(elasticJobConfigBean);
		return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
	}

	@RequestMapping(method = RequestMethod.PUT)
	public ResponseEntity<ResultObject> updateElasticJobConfig(@RequestBody ElasticJobConfigBean elasticJobConfigBean) {
		ElasticJobConfigBean result = elasticJobService.updateElasticJobConfig(elasticJobConfigBean);
		return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
	}

	@RequestMapping(value="/{id}", method = RequestMethod.GET)
	public ResponseEntity<ResultObject> getElasticJobConfig(@PathVariable("id") Long id) {
		ElasticJobConfigBean result = elasticJobService.getElasticJobConfigById(id);
		return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
	}

	@RequestMapping(value="/{id}", method = RequestMethod.DELETE)
	public ResponseEntity<ResultObject> delete(@PathVariable("id") Long id) {
		elasticJobService.delete(id);
		return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
	}

	/**
	 * 重新加载任务
	 * @param jobName 任务名
	 */
	@ApiOperation("重新加载任务")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "jobName", value = "任务名（为空时，重新加载全部任务）", dataType = "string", paramType = "query")
	})
	@RequestMapping(value="/reload", method = RequestMethod.POST)
	public ResponseEntity<ResultObject> reload(@RequestParam("jobName") Optional<String> jobName) {
		boolean reloaded = false;
		Iterable<ElasticJobConfigBean> elasticJobConfigList = elasticJobService.getElasticJobConfigList(jobName.orElse(null));
		if (elasticJobConfigList != null) {
			elasticJobConfigList.forEach(elasticJobConfig -> {
				elasticJobService. registryJob(elasticJobConfig);
				ElasticJobRunner.setJobConfig(elasticJobConfig.getJobName(), elasticJobConfig.getJobConfig());
			});
			reloaded = true;
		}
		return new ResponseEntity<>(new ResultObject<>(reloaded), HttpStatus.OK);
	}

}
